package com.javaweb.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.admin.entity.*;
import com.javaweb.admin.mapper.*;
import com.javaweb.admin.query.ReturnQuery;
import com.javaweb.admin.service.IReturnOrderService;
import com.javaweb.admin.vo.returnorder.OrderReturnDto;
import com.javaweb.admin.vo.returnorder.StoreNameVo;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.system.utils.ShiroUtils;
import freemarker.core.ReturnInstruction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.Store;
import java.math.BigDecimal;
import java.rmi.server.ExportException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReturnOrderServiceImpl extends BaseServiceImpl<ReturnOrderMapper,ReturnOrder> implements IReturnOrderService {
    @Autowired
    private ReturnOrderMapper returnOrderMapper;
    @Autowired
    private SOSaleorderReturnMapper saleorderReturnMapper;
    @Autowired
    private SOSaleorderReturnBMapper saleorderReturnBMapper;
    @Autowired
    private SOResaleInvoiceMapper resaleInvoiceMapper;
    @Autowired
    private SOSaleorderMapper saleorderMapper;
    @Autowired
    private SOSaleorderBMapper saleorderBMapper;

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getReturnOrderList(BaseQuery query) {
        ReturnQuery returnQuery = (ReturnQuery) query;
        IPage<ReturnOrder> page = new Page<>(query.getPage(), query.getLimit());
        IPage<ReturnOrder> pageData = returnOrderMapper.getReturnOrderList(page, returnQuery);
        return JsonResult.success(pageData);

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getReturnSaleOrderList(BaseQuery query) {
        ReturnQuery returnQuery = (ReturnQuery) query;
        IPage<SaleOrder> page = new Page<>(query.getPage(), query.getLimit());
        IPage<SaleOrder> pageData = returnOrderMapper.getReturnSaleOrderList(page, returnQuery);
        return JsonResult.success(pageData);

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getSecondSaleList(BaseQuery query) {
        ReturnQuery returnQuery = (ReturnQuery) query;
        IPage<ReturnOrderDetail> page = new Page<>(query.getPage(), query.getLimit());
        IPage<ReturnOrderDetail> pageData = returnOrderMapper.getSecondSaleList(page, returnQuery);
        return JsonResult.success(pageData);

    }

    /*获取二销可售药品的库存数量*/
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getSecondSaleStockList(BaseQuery query) {
        ReturnQuery returnQuery = (ReturnQuery) query;
        IPage<ReturnOrderDetail> page = new Page<>(query.getPage(), query.getLimit());
        IPage<ReturnOrderDetail> pageData = returnOrderMapper.getSecondSaleStockList(page, returnQuery);
        return JsonResult.success(pageData);

    }

    /*获取二销可售药品的库存数量*/
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getSecondSaleGroupStockList(BaseQuery query) {
        ReturnQuery returnQuery = (ReturnQuery) query;
        IPage<ReturnOrderDetail> page = new Page<>(query.getPage(), query.getLimit());
        IPage<ReturnOrderDetail> pageData = returnOrderMapper.getSecondSaleGroupStockList(page, returnQuery);
        return JsonResult.success(pageData);

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getSecondSaleInvoiceList(BaseQuery query) {
        ReturnQuery returnQuery = (ReturnQuery) query;
        IPage<ReturnOrderInvoiceDetail> page = new Page<>(query.getPage(), query.getLimit());
        IPage<ReturnOrderInvoiceDetail> pageData = returnOrderMapper.getSecondSaleInvoiceList(page, returnQuery);
        return JsonResult.success(pageData);

    }


    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getOrderDetail(String cgeneralhid) {
        List<ReturnOrderDetail> list = returnOrderMapper.gerOrderDetail(cgeneralhid);
        return JsonResult.success(list);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getSaleOrderDetail(String csaleorderid) {
        List<SaleOrderDetail> list = returnOrderMapper.gerSaleOrderDetail(csaleorderid);
        return JsonResult.success(list);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getOrderInvoiceDetail(String csaleorderid) {
        List<ReturnOrderInvoiceDetail> list = returnOrderMapper.gerOrderInvoiceDetail(csaleorderid);
        return JsonResult.success(list);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getStoreList() {
        List<StoreNameVo> list = returnOrderMapper.getStoreNameList();
        return JsonResult.success(list);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getSSBKList() {
        List<String> list = returnOrderMapper.getSSBKList();
        return JsonResult.success(list);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getOrderCheckDetail(String csaleorderid) {
        List<ReturnOrderCheck> list = returnOrderMapper.gerOrderCheckDetail(csaleorderid);
        return JsonResult.success(list);
    }
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getSaleOrderCheckDetail(String csaleorderid) {
        List<ReturnOrderCheck> list = returnOrderMapper.gerSaleOrderCheckDetail(csaleorderid);
        return JsonResult.success(list);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getResaleList(String csaleorderbid) {
        List<ReturnOrderInvoiceDetail> list = returnOrderMapper.getResaleList(csaleorderbid);
        return JsonResult.success(list);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getInstockCheckList(String csaleinvoicebid) {
        List<ReturnOrderInvoiceDetail> list = returnOrderMapper.getInstockCheckList(csaleinvoicebid);
        return JsonResult.success(list);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult saveOrderReturn(OrderReturnDto returnDto) {
        try {
            String csaleorderid =returnDto.getCsaleorderid();
            String csaleorderbid = returnDto.getCsaleorderbid();
            String cgeneralhid =returnDto.getCgeneralhid();
            String cgeneralbid = returnDto.getCgeneralbid();
            String realreason=returnDto.getRealreason();
            String opertype= returnDto.getOpertype();
            String remark = returnDto.getRemark();
            String storename = returnDto.getStorename();
            if (!StringUtils.isEmpty(cgeneralbid)) {
                Map<String, Object> param = new HashMap<>();
                param.put("cgeneralbid", cgeneralbid);
                //param.put("storename", storename);
                List<SOSaleorderReturnB> list = saleorderReturnBMapper.selectByMap(param);
                if (list.size() > 0) {
                    UpdateWrapper<SOSaleorderReturnB> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.set("realreason", realreason);
                    updateWrapper.set("opertype", opertype);
                    updateWrapper.set("remark", remark);
                    updateWrapper.eq("cgeneralbid", cgeneralbid);
                    //updateWrapper.eq("storename", storename);
                    saleorderReturnBMapper.update(new SOSaleorderReturnB(), updateWrapper);
                }
                else
                {
                    SOSaleorderReturnB saleorderReturnB = new SOSaleorderReturnB();
                    saleorderReturnB.setCsaleorderid(csaleorderid);
                    saleorderReturnB.setCsaleorderbid(csaleorderbid);
                    saleorderReturnB.setCgeneralhid(cgeneralhid);
                    saleorderReturnB.setCgeneralbid(cgeneralbid);
                    saleorderReturnB.setRealreason(realreason);
                    saleorderReturnB.setOpertype(opertype);
                    saleorderReturnB.setRemark(remark);
                    saleorderReturnB.setMark(1);
                    saleorderReturnB.setCreateUser(ShiroUtils.getUserId());

                    saleorderReturnBMapper.insert(saleorderReturnB);

                }

            } else {
                Map<String, Object> param = new HashMap<>();
                //param.put("csaleorderid", csaleorderid);
                //param.put("storename", storename);
                param.put("cgeneralhid", cgeneralhid);

                SOSaleorderReturn saleorderReturn = new SOSaleorderReturn();
                List<SOSaleorderReturn> list = saleorderReturnMapper.selectByMap(param);
                if(list.size()>0){
                    UpdateWrapper<SOSaleorderReturn> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.set("realreason", realreason);
                    updateWrapper.set("opertype", opertype);
                    updateWrapper.set("remark", remark);
                    updateWrapper.eq("cgeneralhid", cgeneralhid);
                    //updateWrapper.eq("storename", storename);
                    saleorderReturnMapper.update(new SOSaleorderReturn(), updateWrapper);
                    UpdateWrapper<SOSaleorderReturnB> updateWrapperB = new UpdateWrapper<>();
                    updateWrapperB.set("realreason", realreason);
                    updateWrapperB.set("opertype", opertype);
                    updateWrapperB.set("remark", remark);
                    updateWrapperB.eq("cgeneralhid", cgeneralhid);
                    //updateWrapperB.eq("csaleorderid", csaleorderid);
                    //updateWrapperB.eq("storename", storename);
                    saleorderReturnBMapper.update(new SOSaleorderReturnB(), updateWrapperB);

                }else {
                    saleorderReturn.setCsaleorderid(csaleorderid);
                    saleorderReturn.setCgeneralhid(cgeneralhid);
                    saleorderReturn.setRealreason(realreason);
                    saleorderReturn.setOpertype(opertype);
                    saleorderReturn.setRemark(remark);
                    saleorderReturn.setStorename(storename);
                    saleorderReturn.setMark(1);
                    saleorderReturn.setCreateUser(ShiroUtils.getUserId());
                    saleorderReturnMapper.insert(saleorderReturn);
                    SOSaleorderReturnB saleorderReturnB = null;
                    List<ReturnOrderDetail> listB = returnOrderMapper.gerOrderDetail(cgeneralhid);

                    for (ReturnOrderDetail item : listB) {
                        saleorderReturnB = new SOSaleorderReturnB();
                        saleorderReturnB.setCsaleorderid(csaleorderid);
                        saleorderReturnB.setCsaleorderbid(item.getCsaleorderbid());
                        saleorderReturnB.setCgeneralhid(item.getCgeneralhid());
                        saleorderReturnB.setCgeneralbid(item.getCgeneralbid());
                        saleorderReturnB.setRealreason(realreason);
                        saleorderReturnB.setOpertype(opertype);
                        saleorderReturnB.setRemark(remark);
                        saleorderReturnB.setStorename(storename);
                        saleorderReturnB.setMark(1);
                        saleorderReturnB.setCreateUser(ShiroUtils.getUserId());

                        saleorderReturnBMapper.insert(saleorderReturnB);

                    }
                }


            }
            return JsonResult.success();
        }
        catch(Exception ex)
        {
            return JsonResult.error("退货操作失败");
        }

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult saveSaleOrderReturn(OrderReturnDto returnDto) {
        try {
            String csaleorderid =returnDto.getCsaleorderid();
            String csaleorderbid = returnDto.getCsaleorderbid();
            String cgeneralhid =returnDto.getCgeneralhid();
            String cgeneralbid = returnDto.getCgeneralbid();
            String realreason=returnDto.getRealreason();
            String opertype= returnDto.getOpertype();
            String remark = returnDto.getRemark();
            String storename = returnDto.getStorename();
            if (!StringUtils.isEmpty(cgeneralbid)) {
                Map<String, Object> param = new HashMap<>();
                param.put("csaleorderid", cgeneralbid);
                //param.put("storename", storename);
                List<SOSaleorderB> list = saleorderBMapper.selectByMap(param);
                if (list.size() > 0) {
                    UpdateWrapper<SOSaleorderB> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.set("realreason", realreason);
                    updateWrapper.set("opertype", opertype);
                    updateWrapper.set("remark", remark);
                    updateWrapper.eq("csaleorderid", csaleorderid);
                    //updateWrapper.eq("storename", storename);
                    saleorderBMapper.update(new SOSaleorderB(), updateWrapper);
                }
                else
                {
                    SOSaleorderB saleorderB = new SOSaleorderB();
                    saleorderB.setCsaleorderid(csaleorderid);
                    saleorderB.setCsaleorderbid(csaleorderbid);
                    saleorderB.setRealreason(realreason);
                    saleorderB.setOpertype(opertype);
                    saleorderB.setRemark(remark);
                    saleorderB.setMark(1);
                    saleorderB.setCreateUser(ShiroUtils.getUserId());

                    saleorderBMapper.insert(saleorderB);

                }

            } else {
                Map<String, Object> param = new HashMap<>();
                //param.put("csaleorderid", csaleorderid);
                //param.put("storename", storename);
                param.put("csaleorderid", csaleorderid);

                SOSaleorder saleorder = new SOSaleorder();
                List<SOSaleorder> list = saleorderMapper.selectByMap(param);
                if(list.size()>0){
                    UpdateWrapper<SOSaleorder> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.set("realreason", realreason);
                    updateWrapper.set("opertype", opertype);
                    updateWrapper.set("remark", remark);
                    updateWrapper.eq("csaleorderid", csaleorderid);
                    //updateWrapper.eq("storename", storename);
                    saleorderMapper.update(new SOSaleorder(), updateWrapper);
                    UpdateWrapper<SOSaleorderB> updateWrapperB = new UpdateWrapper<>();
                    updateWrapperB.set("realreason", realreason);
                    updateWrapperB.set("opertype", opertype);
                    updateWrapperB.set("remark", remark);
                    updateWrapperB.eq("csaleorderid", csaleorderid);
                    //updateWrapperB.eq("csaleorderid", csaleorderid);
                    //updateWrapperB.eq("storename", storename);
                    saleorderBMapper.update(new SOSaleorderB(), updateWrapperB);

                }else {
                    saleorder.setCsaleorderid(csaleorderid);
                    saleorder.setRealreason(realreason);
                    saleorder.setOpertype(opertype);
                    saleorder.setRemark(remark);
                    saleorder.setStorename(storename);
                    saleorder.setMark(1);
                    saleorder.setCreateUser(ShiroUtils.getUserId());
                    saleorderMapper.insert(saleorder);
                    SOSaleorderB saleorderB = null;
                    List<SaleOrderDetail> listB = returnOrderMapper.gerSaleOrderDetail(csaleorderid);

                    for (SaleOrderDetail item : listB) {
                        saleorderB = new SOSaleorderB();
                        saleorderB.setCsaleorderid(csaleorderid);
                        saleorderB.setCsaleorderbid(item.getCsaleorderbid());
                        saleorderB.setRealreason(realreason);
                        saleorderB.setOpertype(opertype);
                        saleorderB.setRemark(remark);
                        saleorderB.setStorename(storename);
                        saleorderB.setMark(1);
                        saleorderB.setCreateUser(ShiroUtils.getUserId());

                        saleorderBMapper.insert(saleorderB);

                    }
                }


            }
            return JsonResult.success();
        }
        catch(Exception ex)
        {
            return JsonResult.error("退货操作失败");
        }

    }


    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult delResaleInvoice(String cgeneralbid) {
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("cgeneralbid", cgeneralbid);
            resaleInvoiceMapper.deleteByMap(param);
            return JsonResult.success();
        }
        catch(Exception ex)
        {
            return JsonResult.error("删除失败");
        }

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult delResaleInstock(String csaleinvoicebid,String cgeneralbid) {
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("csaleinvoicebid", csaleinvoicebid);
            param.put("cgeneralbid", cgeneralbid);
            resaleInvoiceMapper.deleteByMap(param);
            return JsonResult.success();
        }
        catch(Exception ex)
        {
            return JsonResult.error("删除失败");
        }

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult saveResaleInvoice(SOResaleInvoice[] resaleInvoices) {
        try {
            BigDecimal nnum = BigDecimal.ZERO;
            for (SOResaleInvoice resaleInvoice : resaleInvoices) {
                Map<String, Object> param = new HashMap<>();
                param.put("cgeneralbid", resaleInvoice.getCgeneralbid());
                param.put("csaleinvoicebid", resaleInvoice.getCsaleinvoicebid());
                List<SOResaleInvoice> list = resaleInvoiceMapper.selectByMap(param);
                nnum = resaleInvoice.getNnum();
                if (list.size() == 0) {
                    if(nnum.compareTo(BigDecimal.ZERO)>0) {
                        resaleInvoice.setMark(1);
                        resaleInvoice.setOpername(ShiroUtils.getUserName());
                        resaleInvoice.setCreateUser(ShiroUtils.getUserId());
                        resaleInvoice.setCreateTime(DateUtils.now());
                        resaleInvoiceMapper.insert(resaleInvoice);
                    }
                }
                else
                {
                    BigDecimal num = resaleInvoice.getNnum();
                    SOResaleInvoice resaleInvoice1 = list.get(0);
                    resaleInvoice1.setNnum(num.add(resaleInvoice1.getNnum()));
                    resaleInvoiceMapper.updateById(resaleInvoice1);
                }
            }
            return JsonResult.success();
        }
        catch(Exception ex)
        {
            return JsonResult.error("操作失败");
        }

    }

}
